/*
 * Decoder
 *
 *  Created on: 10/06/2011
 *      Author: Agustina I. Stekolschik & Ezequiel Palumbo
 */

#ifndef DECODER_H_
#define DECODER_H_

#include <AbstractContext.h>
#include <DataUnit.h>
#include <DataInputOutput.h>

using namespace contexts;
using namespace DataInputOutput;

namespace decoder{

class Decoder{
public:
	Decoder(){};
	virtual DataUnit* decodeNext(AbstractContext& currentContext)=0;
	virtual void setDataInput(BitDataInput& dataInput)=0;
	virtual void setDataUnitFactory(DataUnitFactory& dataUnitFactory) =0;
	virtual ~Decoder(){};
protected:
	BitDataInput* bitDataInput;
	DataUnitFactory* dataUnitFactory;
};

class ArithmeticDecoder : public Decoder{
public:
	ArithmeticDecoder();
	DataUnit* decodeNext(AbstractContext& currentContext);
	void setDataInput(BitDataInput& dataInput);
	virtual void setDataUnitFactory(DataUnitFactory& dataUnitFactory);
	virtual ~ArithmeticDecoder();
protected:
	unsigned char getBit();
	void decode( const unsigned int low_count,const unsigned int high_count );
	unsigned int decodeTarget( const unsigned int total );
	void decodeStart();

	unsigned int lowBound;
	unsigned int topBound;
	unsigned int currStep;
	unsigned int currScale;
	unsigned int buffer;

};

}
#endif
